#include <OgreVector3.h>
#include <OgreSceneManager.h>
#include <OgreSceneNode.h>
#include <OgreManualObject.h>
#include <OgreMaterialManager.h>
#include "Sphere.h"
#include <math.h>
#include <time.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include "Poly.h"
#include "Globals.h"

#pragma once
//#pragma comment(lib, "irrKlang.lib") // link with irrKlang.dll

#ifndef WORLD_H
#define WORLD_H

class PolyFactory;
class World
{
public:
	World(Ogre::SceneManager *sceneManager);
	virtual ~World(void);
	void worldStep(const Ogre::FrameEvent& evt);
	void createSphere(Ogre::Vector3 initPosition, Ogre::Vector3 initSpeed);
	void addPolygonToWorld(Poly *polygon);

	void generateLevel();
	void switchLevelOne();
	void switchLevelTwo();
	void reset();

	void setPhysics(int physicsComponent, Ogre::Real physicsValue);
	Ogre::Real getPhysics(int physicsComponent);
	void setGameState(int gameState); //"START", "STOP", "PAUSE", "RESET"
	int getGameState();
	int getCurrentLevel(){return m_currentLevel;};
	void setCurrentLevel(int level){m_currentLevel = level;};

	std::vector<Poly*> m_polygons;
	std::vector<Sphere*> m_sceneElements;
	Sphere *m_currentSphere;
	Ogre::SceneManager *m_sceneManager;	

private:
	void checkWorldCollisions(const Ogre::FrameEvent& evt);
	void frameLagCompensation(Sphere *sphereA, Sphere* speherB, const Ogre::FrameEvent& evt);
	void checkPolyCollision(Sphere *sphere, const Ogre::FrameEvent& evt);
	Ogre::Vector2 intersectsOnPoint(Ogre::Vector2 p0, Ogre::Vector2 p1, Ogre::Vector2 p2, Ogre::Vector2 p3);
	void drawPolygons();

	//irrklang::ISoundEngine *m_engine;

	int m_currentLevel;
	PolyFactory *m_polyFactory;
	time_t m_initialTime;
	bool m_targetHit;
	int m_gameState;
	Ogre::Real m_gravity;
	Ogre::Real m_bounce;
	Ogre::Real m_friction;
};
#endif
